<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/23
 * Time: 15:42
 */

namespace Api\Controller;

use Think\Controller;

class CarController extends Controller
{
    /**
     * 加入购物车
     */
    public function addCar()
    {
        $data['customer_id'] = $this->get_customer_id();
        $customer_id = $data['customer_id'];
        $data['good_id'] = I('post.good_id');
        $goods_id = $data['good_id'];

        $data['category_name'] = I('request.category_name');
        $data['category_id'] = I('request.category_id');
        $data['good_name'] = I('request.good_name');
        $data['good_price'] = I('request.good_price');
        $data['good_pic'] = I('request.good_pic');
        $data['count'] = I('request.count');
        $data['practice'] = I('request.practice');
        $data['table_id'] = I('request.table_id');
        $data['time'] =time();
        if (!empty($goods_id) && !empty($customer_id)) {
            //判断购物车是否有商品,如果没有,商品数量1,如果有+1

            $data['time'] = time();
            $result = M('car')->add($data);
            if ($result) {
                //加入购物车成功
                $this->ajaxreturn(array('state' => 1, 'data' => $data), 'json');
            } else {
                //加入购物车失败
                $this->ajaxreturn(array('state' => -1), 'json');
            }
        } else {
            //没有拿到商品ID
            $this->ajaxreturn(array('state' => 0), 'json');
        }

    }

    /**
     * 购物车列表
     */
    public function carList()
    {
        $user_id = $this->get_customer_id();

        $time = time();
        if (!empty($user_id)) {
            $result = M('car')->alias('c')->where("c.customer_id='$user_id'")->select();

            foreach ($result as $key => $value) {
                $result[$key]['formart_start_time'] = date('Y-m-d', $value['start_time']);
                $result[$key]['formart_end_time'] = date('Y-m-d', $value['end_time']);
                if ($value['start_time'] <= $time) {
                    $result[$key]['lost'] = $value['car_id'];
                }
            }
//            $this->ajaxreturn(M()->_sql());
            if ($result) {
                //购物车的东西
                $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
            } else {
                //没查到东西
                $this->ajaxreturn(array('state' => 0), 'json');
            }
        } else {
            //user_id不存在
            $this->ajaxreturn(array('state' => -1), 'json');
        }
    }

    /**
     * 更新购物车
     */
    public function carUpdate()
    {
        $user_id = $this->get_customer_id();
        $car_id = I('post.id');
        $num = I('post.num');
        if ($user_id && $car_id) {
            $data['num'] = $num;
            $result = M('car')->where("id='$car_id'")->save($data);
            if ($result) {
                //更新成功
                $this->ajaxreturn(array('state' => 1, 'data' => $data), 'json');
            } else {
                //更新失败(为改变)
                $this->ajaxreturn(array('state' => -1), 'json');
            }
        } else {
            //user_id或者car_id丢失
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 删除购物车
     */
    public function delCar()
    {
        $car_id = I('request.car_id');
        if ($car_id) {
            $result = M('car')->where("id='$car_id'")->delete();
            if ($result) {
                //删除成功
                $this->ajaxreturn(array('state' => 1), 'json');
            } else {
                //删除失败
                $this->ajaxreturn(array('state' => -1), 'json');
            }
        } else {
            //缺少购物车ID
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 获取购物车数量
     */
    public function getCarnum()
    {
        $user_id = $this->get_customer_id();
        $result = M('car')->where("customer_id='$user_id'")->field(array("sum(num)" => 'all_num', 'goods_id'))->find();
        if (empty($result['all_num'])) {
            $result['all_num'] = 0;
        }
        if ($user_id) {
            if ($result) {
                //获取购物车的总数量和商品ID
                $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
            } else {
                //购物车还没有商品
                $this->ajaxreturn(array('state' => -1, 'data' => $result), 'json');
            }
        } else {
            //没登录
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 清空购物车
     */
    public function del(){
        $user_id=$this->get_customer_id();
        $result=M('car')->where("customer_id='$user_id'")->delete();
        if($result){
            $this->ajaxreturn(array('state'=>1),'json');
        }else{
            $this->ajaxreturn(array('state'=>0),'json');
        }
    }
}